﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework;
using FantastPlatform.Framework.IService;
using NHibernate;
using FantastPlatform.Framework.Model;
using FantastPlatform.Framework.Core;

namespace FantastPlatform.Community.Service
{
    public class ResponsibilityAreaStreetService : DataService<IResponsibilityAreaStreet, ResponsibilityAreaStreet>, IResponsibilityAreaStreetService
    {
        #region IResponsibilityAreaStreetService 成员

        public void AddStreet(int responsibilityAreaId, int streetId)
        {
            IOrganizationService orgService = ServiceFactory.CreateService<IOrganizationService>();
            IStreetService streetService = ServiceFactory.CreateService<IStreetService>();
            IOrganization org = orgService.Get(responsibilityAreaId);
            IList<IResponsibilityAreaStreet> areaStreetList;
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enble", Type = typeof(bool).FullName, Value1 = true });
            group.Conditions.Add(new Condition { Field = "ResponsibilityArea.Id", Type = typeof(Int32).FullName, Value1 = responsibilityAreaId });
            if (org != null)
            {
                IResponsibilityAreaStreet areaStreet = new ResponsibilityAreaStreet();
                areaStreet.ResponsibilityArea = org as Organization;
                group.Conditions.Add(new Condition { Field = "Street.Id", Type = typeof(Int32).FullName, Value1 = streetId });
                int totalCount = 0;
                areaStreetList = base.Search(group, null, Int32.MaxValue, 1, out totalCount);
                if (totalCount == 0)
                {
                    areaStreet.Street = streetService.Get(streetId);
                    if (areaStreet.Street != null)
                    {
                        areaStreet.Name = areaStreet.Street.Name;
                        base.Create(areaStreet);
                    }
                }

            }

        }

        public void RemoveStreet(int responsibilityAreaId, List<int> streetIds)
        {

            IList<IResponsibilityAreaStreet> areaStreetList;
            List<int> areaStreeIds = new List<int>();
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enble", Type = typeof(bool).FullName, Value1 = true });
            group.Conditions.Add(new Condition { Field = "ResponsibilityArea.Id", Type = typeof(Int32).FullName, Value1 = responsibilityAreaId });
            foreach (int streetId in streetIds)
            {
                group.Conditions.Add(new Condition { Field = "Street.Id", Type = typeof(Int32).FullName, Value1 = streetId });
                int totalCount = 0;
                areaStreetList = base.Search(group, null, Int32.MaxValue, 1, out totalCount);
                if (totalCount > 0)
                {
                    foreach (IResponsibilityAreaStreet item in areaStreetList)
                    {
                        areaStreeIds.Add(item.Id);
                    }

                }
            }
            base.Delete(areaStreeIds);

        }

        #endregion
    }
}
